package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BConference;
import com.ruoyi.system.domain.vo.BConferenceVo;
import com.ruoyi.system.service.BCaseConferenceService;
import com.ruoyi.system.service.BConferenceService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 研讨会管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/conference")
public class ConferenceController extends BaseController {


    @Autowired
    BConferenceService conferenceService;

    @Autowired
    BCaseConferenceService caseConferenceService;

    /**
     * 获取研讨会列表
     */
    @PreAuthorize("@ss.hasPermi('dict:conference:list')")
    @GetMapping("/list")
    public TableDataInfo list(BConferenceVo conferenceVo)
    {
        startPage();
        List<BConferenceVo> list = conferenceService.selectConferenceList(conferenceVo);
        return getDataTable(list);
    }

    /**
     * 根据研讨会Id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('dict:conference:query')")
    @GetMapping(value = "/{conferenceId}")
    public AjaxResult getInfo(@PathVariable Long conferenceId) {
        BConferenceVo conferenceVo = conferenceService.selectConferenceById(conferenceId);
        return success(conferenceVo);
    }

    /**
     * 新增研讨会
     */
    @PreAuthorize("@ss.hasPermi('dict:conference:add')")
    @Log(title = "新增研讨会", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BConferenceVo conferenceVo)
    {
        BConference conference = new BConference();
        BeanUtils.copyProperties(conferenceVo,conference);
        conference.setCreateBy(getUsername());
        conference.setCreateTime(new Date());
        boolean result = conferenceService.save(conference);
        if(result){
            //创建关联关系
            if(ObjectUtils.isNotEmpty(conferenceVo.getCaseIds())){
                caseConferenceService.createCaseConference(conference.getId(),conferenceVo.getCaseIds());
            }
        }
        return AjaxResult.success(conference);
    }

    /**
     * 修改研讨会
     */
    @PreAuthorize("@ss.hasPermi('dict:conference:edit')")
    @Log(title = "修改研讨会", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BConferenceVo conferenceVo)
    {
        BConference conference = new BConference();
        BeanUtils.copyProperties(conferenceVo,conference);
        conference.setUpdateBy(getUsername());
        conference.setUpdateTime(new Date());
        boolean result = conferenceService.updateById(conference);
        if(result){
            //创建关联关系
            if(ObjectUtils.isNotEmpty(conferenceVo.getCaseIds())){
                caseConferenceService.createCaseConference(conference.getId(),conferenceVo.getCaseIds());
            }
        }
        return toAjax(conferenceService.updateById(conference));
    }

    /**
     * 删除研讨会
     */
    @PreAuthorize("@ss.hasPermi('dict:conference:remove')")
    @Log(title = "删除研讨会", businessType = BusinessType.DELETE)
    @DeleteMapping("/{conferenceIds}")
    public AjaxResult remove(@PathVariable Long[] conferenceIds)
    {
        return toAjax(conferenceService.deleteConferenceByIds(conferenceIds));
    }
}
